package com.wyp.didishige.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bmob.pay.tool.BmobPay;
import com.bmob.pay.tool.PayListener;
import com.wyp.didishige.MyApplication;
import com.wyp.didishige.utils.ToastUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.model.User;
import com.wyp.didishige.utils.PreferenceHelper;

import java.sql.Date;

import cn.bmob.v3.listener.UpdateListener;

public class PayActivity extends BaseActivity implements View.OnClickListener{
    RadioButton one,three,six,year;

    Button payBtn;
    TextView payAccount;//支付金额

    String payTag="个月会员";

    int level=0;//会员等级

    int account = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        actionBar=getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setTitle("购买会员");
        one= (RadioButton) findViewById(R.id.one);
        three= (RadioButton) findViewById(R.id.three);
        six= (RadioButton) findViewById(R.id.six);
        year= (RadioButton) findViewById(R.id.year);
        payAccount = (TextView) findViewById(R.id.payAccount);
        payBtn = (Button) findViewById(R.id.payBtn);
        one.setOnClickListener(this);
        three.setOnClickListener(this);
        six.setOnClickListener(this);
        year.setOnClickListener(this);
        payBtn.setOnClickListener(this);
        MyApplication.add(this);
    }

    @Override
    public void onClick(View v) {
        final Bundle bundle = null;
        switch (v.getId()){
            case R.id.one:
                level =1;
                payAccount.setText("12元");
                account=12;
                one.setChecked(true);
                three.setChecked(false);
                six.setChecked(false);
                year.setChecked(false);
                break;
            case R.id.three:
                level=3;
                payAccount.setText("30元");
                account=30;
                one.setChecked(false);
                three.setChecked(true);
                six.setChecked(false);
                year.setChecked(false);
                break;
            case R.id.six:
                level=6;
                payAccount.setText("60元");
                account =60;
                one.setChecked(false);
                three.setChecked(false);
                six.setChecked(true);
                year.setChecked(false);
                break;
            case R.id.year:
                level=12;
                payAccount.setText("108元");
                account =108;
                one.setChecked(false);
                three.setChecked(false);
                six.setChecked(false);
                year.setChecked(true);
                break;
            case R.id.payBtn:
                if (level == 0){
                    ToastUtil.showShort(this, "请选择会员种类");
                    break;
                }
                pay();
                break;

        }
    }

    private void pay(){
        new BmobPay(this).pay(0.02, level + payTag, new PayListener() {
            @Override
            public void orderId(String s) {
            }

            @Override
            public void succeed() {
                PreferenceHelper.write(PayActivity.this, Config.PreferenceHelperfile, "vip", true);
                PreferenceHelper.write(PayActivity.this, Config.PreferenceHelperfile, "viplevel", level);
                update();
            }

            @Override
            public void fail(int i, String s) {

                String error = "";
                switch (i) {
                    case 6001:
                        error= "支付已取消";
                        break;
                    default:
                        error = "支付失败";
                        break;
                }
                ShowToast(error);
            }

            @Override
            public void unknow() {
                ShowToast("未知错误");
            }
        });
    }

    private void update() {
        User user = userManager.getCurrentUser(User.class);
        Date dueDate = user.getDueTime();
        if (dueDate==null){
            dueDate = new Date(System.currentTimeMillis());
        }
        dueDate.setMonth(dueDate.getMonth()+level);
        user.setDueTime(dueDate);
        user.setLevel(level);
        user.update(this, new UpdateListener() {
            @Override
            public void onSuccess() {
            }

            @Override
            public void onFailure(int i, String s) {
                ShowToast(s);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
